<?php

namespace App\Admin\Controllers;

use App\Models\Standard;
use App\Models\Business;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class StandardController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '评价标准管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Standard);

        $grid->model()->orderBy('start_time', 'desc');

        $grid->column('id', 'ID');
        $grid->column('business_id', '业务名称')->display(function () {
            return $this->business->name;
        });

        $grid->column('name', '评价标准');

        $grid->column('limit', '评价区间')->display(function () {
            if ($this->name == '优秀') {
                return $this->lower_limit . '分以上';
            } elseif ($this->name == '重点关注') {
                return $this->upper_limit . '分以下';
            }
            return '['.$this->lower_limit.'分(包含)--'.$this->upper_limit.'分(不含)';
        })->label('success');

        $grid->column('start_time', '开始时间')->date('Y-m-d');

        $grid->column('end_time', '结束时间')->date('Y-m-d');

        $grid->filter(function ($filter) {
            $filter->column(1/2, function ($filter) {
                $filter->disableIdFilter();
                $filter->equal('business_id', '业务类型')->select('businesses/getBusiness');
                $filter->equal('name', '评价标准')->select([
                    '优秀'=>'优秀',
                    '良好'=>'良好',
                    '一般'=>'一般',
                    '关注'=>'关注',
                    '重点关注'=>'重点关注',
                ]);
            });
            $filter->column(1/2, function ($filter){
                $filter->between('start_time', '开始时间')->date();
                $filter->between('end_time', '结束时间')->date();
            });

        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Standard::findOrFail($id));



        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Standard);

        $form->display('id','ID');
        $form->select('name','标准名称')->options([
            '优秀'=>'优秀',
            '良好'=>'良好',
            '一般'=>'一般',
            '关注'=>'关注',
            '重点关注'=>'重点关注',
        ]);
        $form->select('business_id','业务类型')->options(Business::selectOptions());

        $form->number('lower_limit','评分下限(包含)');
        $form->number('upper_limit','评分上限(不包含)');

        $form->dateRange('start_time', 'end_time', '业务持续时间');

        return $form;
    }
}
